package com.hks360.library.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hks360.library.Charset;

public class CommonUtil {
	public static void openActivity(Context context, Class<?> activityClass) {
		Intent intent = new Intent();
		intent.setClass(context, activityClass);
		context.startActivity(intent);
	}
	public static void openActivityForResult(Activity context,Class<?> activityClass,int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context,activityClass);
		context.startActivityForResult(intent,requestCode);
	}

	public static Intent getIntent(Context context, Class<?> activityClass) {
		Intent intent = new Intent();
		intent.setClass(context, activityClass);
		return intent;
	}

	public static void showToast(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	public static void showToast(Context context, int textResId) {
		Toast.makeText(context, textResId, Toast.LENGTH_SHORT).show();
	}

	public static boolean isEmptyEt(EditText edit) {
		if (edit == null || edit.getText().toString().trim().length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isEmptyTv(TextView textView) {
		if (textView == null || textView.getText().toString().trim().length() == 0) {
			return true;
		}
		return false;
	}

	public static PopupWindow createPopupWindow(View view) {
		PopupWindow popupWindow = new PopupWindow(view);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		popupWindow.setFocusable(true);
		return popupWindow;
	}

	public static void showPopup(PopupWindow popupWindow, View view) {
		if (popupWindow.isShowing()) {
			popupWindow.dismiss();
		} else {
			popupWindow.showAsDropDown(view);
		}
	}

	public static boolean isEmptyMap(Map<?, ?> map) {
		if (map == null || map.isEmpty()) {
			return true;
		}
		return false;
	}

	public static boolean isEmptyStr(String str) {
		if (str == null || str.length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isEmptyList(List<?> list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	public static boolean isEmptyArray(Object[] obj) {
		if (obj == null || obj.length == 0) {
			return true;
		}
		return false;
	}

	public static boolean isExistApp(Context context, String packageName) {
		boolean flag = false;
		PackageManager pm = context.getPackageManager();
		try {
			PackageInfo pi = pm.getPackageInfo(packageName, 0);
			if (pi != null) {
				flag = true;
			}
		} catch (PackageManager.NameNotFoundException e) {
			flag = false;
		}
		return flag;
	}

	public static void installApk(Context context, String filePath) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	public static String getFileSize(long fileSize) {
		DecimalFormat format = new DecimalFormat("0.0");
		if (fileSize < 1024) {
			return String.valueOf(fileSize) + "B";
		}
		double sizeKB = fileSize * 1.0 / 1024;
		if (sizeKB < 1024) {
			return format.format(sizeKB) + "KB";
		}
		double sizeMB = sizeKB / 1024;
		if (sizeMB < 1024) {
			return format.format(sizeMB) + "MB";
		}
		return fileSize == 0 ? "0" : String.valueOf(fileSize);
	}

	public static final String getURLEncodeStr(String s) {
		try {
			return URLEncoder.encode(s, Charset.UTF8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static final String[] getVersion(Context context, String packageName) {
		try {
			String[] versionArr = new String[2];
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(packageName, 0);
			String versionCode = String.valueOf(info.versionCode);
			String versionName = info.versionName;
			versionArr[0] = versionCode;
			versionArr[1] = versionName;
			return versionArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String digitUppercase(double n) {
		String fraction[] = { "角", "分" };
		String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
		String head = n < 0 ? "负" : "";
		n = Math.abs(n);
		String s = "";
		for (int i = 0; i < fraction.length; i++) {
			s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
		}
		if (s.length() < 1) {
			s = "整";
		}
		int integerPart = (int) Math.floor(n);
		
		for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
			String p = "";
			for (int j = 0; j < unit[1].length && n > 0; j++) {
				p = digit[integerPart % 10] + unit[1][j] + p;
				integerPart = integerPart / 10;
			}
			s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
		}
		return head	+ s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零")
										.replaceAll("^整$", "零元整");
	}
}
